<template>
  <div>
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <!-- v-model="activeName" @tab-click="handleClick"-->
        <!--头部左侧-->
        <el-tabs class="tab" v-model="activeName">
          <el-tab-pane label="销售额" name="sale"></el-tab-pane>
          <el-tab-pane label="访问量" name="visite"></el-tab-pane>
        </el-tabs>
        <!--头部右侧-->
        <div class="right">
          <span @click="setDay">今日</span>
          <span @click="setWeek">本周</span>
          <span @click="setMonth">本月</span>
          <span @click="setYear">本年</span>
          <!--v-model="value"-->
          <el-date-picker
            v-model="date"
            class="data"
            type="daterange"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            :default-time="['00:00:00', '23:59:59']"
            size="small"
            value-format="yyyy-MM-dd"
          >
          </el-date-picker>
        </div>
      </div>
      <!--身体-->
      <div>
        <el-row :gutter="10">
          <el-col :span="18">
            <div class="charts" ref="charts"></div>
          </el-col>
          <el-col :span="6">
            <strong>门店{{ title }}排名</strong>
            <ul class="ranking">
              <li v-for="item in rank" :key="item.no">
                <span
                  :class="{
                    rindex: item.no < 4 ? true : false,
                    rnoindex: item.no < 4 ? false : true,
                  }"
                  >{{ item.no }}</span
                >
                <span>{{ item.name }}</span>
                <span class="rvalue">{{ item.money }}</span>
              </li>
            </ul>
          </el-col>
        </el-row>
      </div>
    </el-card>
  </div>
</template>

<script>
import * as echarts from "echarts";
import dayjs from "dayjs";
import { mapState } from "vuex";
export default {
  name: "",
  data() {
    return {
      activeName: "sale",
      myCharts: "",
      date: [], //收集日历数据
      rank: [], //收集门店排名
    };
  },
  mounted() {
    this.myCharts = echarts.init(this.$refs.charts);
    this.myCharts.setOption({
      title: {
        text: this.title + "趋势",
      },
      tooltip: {
        trigger: "axis",
        axisPointer: {
          type: "shadow",
        },
      },
      grid: {
        left: "3%",
        right: "4%",
        bottom: "3%",
        containLabel: true,
      },
      xAxis: [
        {
          type: "category",
          data: [],
          axisTick: {
            alignWithLabel: true,
          },
        },
      ],
      yAxis: [
        {
          type: "value",
          axisLine: {
            show: true,
          },
        },
      ],
      series: [
        {
          name: "Direct",
          type: "bar",
          barWidth: "60%",
          data: [],
          color: "#66ccff",
        },
      ],
    });
  },
  computed: {
    title() {
      return this.activeName == "sale" ? "销售额" : "访问量";
    },
    ...mapState({
      listState: (state) => state.home.list,
    }),
  },
  watch: {
    title() {
      this.title == "销售额"
        ? (this.rank = this.$store.state.home.list.userRank)
        : (this.rank = this.$store.state.home.list.orderRank);
      this.myCharts.setOption({
        title: {
          text: this.title + "趋势",
        },
        xAxis: {
          data:
            this.title == "销售额"
              ? this.listState.orderFullYearAxis
              : this.listState.userFullYearAxis,
        },
        series: [
          {
            name: "Direct",
            type: "bar",
            barWidth: "60%",
            data:
              this.title == "销售额"
                ? this.listState.orderFullYear
                : this.listState.userFullYear,
            color: "#66ccff",
          },
        ],
      });
    },
    listState() {
      this.title == "销售额"
        ? (this.rank = this.$store.state.home.list.userRank)
        : (this.rank = this.$store.state.home.list.orderRank);
      this.myCharts.setOption({
        title: {
          text: this.title + "趋势",
        },
        tooltip: {
          trigger: "axis",
          axisPointer: {
            type: "shadow",
          },
        },
        grid: {
          left: "3%",
          right: "4%",
          bottom: "3%",
          containLabel: true,
        },
        xAxis: [
          {
            type: "category",
            data: this.listState.orderFullYearAxis,
            axisTick: {
              alignWithLabel: true,
            },
          },
        ],
        yAxis: [
          {
            type: "value",
            axisLine: {
              show: true,
            },
          },
        ],
        series: [
          {
            name: "Direct",
            type: "bar",
            barWidth: "60%",
            data: this.listState.orderFullYear,
            color: "#66ccff",
          },
        ],
      });
    },
  },
  methods: {
    setDay() {
      const day = dayjs().format("YYYY-MM-DD");
      this.$set(this.date, 0, day);
      this.$set(this.date, 1, day);
    },
    setWeek() {
      const start = dayjs().day(1).format("YYYY-MM-DD");
      const end = dayjs().day(7).format("YYYY-MM-DD");
      this.date = [start, end];
    },
    setMonth() {
      const start = dayjs().startOf("month").format("YYYY-MM-DD");
      const end = dayjs().endOf("month").format("YYYY-MM-DD");
      this.date = [start, end];
    },
    setYear() {
      const start = dayjs().startOf("year").format("YYYY-MM-DD");
      const end = dayjs().endOf("year").format("YYYY-MM-DD");
      this.date = [start, end];
    },
  },
};
</script>

<style lang="css" scoped>
.clearfix {
  position: relative;
  display: flex;
  justify-content: space-between;
}
.tab {
  width: 100%;
}
.right {
  position: absolute;
  right: 0;
}
.data {
  width: 230px;
  margin: 0 20px;
}
.right span {
  margin: 0 10px;
  color: rgb(114, 114, 114);
  font-size: 14px;
  cursor: pointer;
}
.charts {
  height: 300px;
  width: 100%;
}
>>> .el-card__body {
  padding-top: 0;
}
ul {
  list-style: none;
  width: 100%;
  height: 300px;
  padding: 0;
}
ul li {
  height: 8%;
  margin: 12px 0;
}
.ranking span {
  line-height: 22px;
  font-size: 15px;
}
.rindex {
  float: left;
  width: 22px;
  height: 22px;
  border-radius: 50%;
  background-color: black;
  color: white;
  text-align: center;
  line-height: 22px;
  margin-right: 20px;
}
.rnoindex {
  float: left;
  width: 22px;
  height: 22px;
  text-align: center;
  line-height: 22px;
  margin-right: 20px;
}
.rvalue {
  float: right;
}
</style>
